using System;
using System.Threading;
public class BintryTree<T>
{
    private T[] data;
    public T[] Data
    {
        get
        {
            return data;
        }
    }

    private int count = 0;

    public BintryTree(int capacity)
    {
        data = new T[capacity];
    }

    public bool Add(T item)
    {
        if (count >= data.Length)
        {
            return false;
        }
        data[count] = item;
        count++;
        return true;
    }

    public void FirstTraversal(Action<int> cb)
    {
        FirstTraversal(0, cb);
    }

    private void FirstTraversal(int index, Action<int> cb)
    {
        if (index >= count || data[index].Equals(-1))
        {
            return;
        }
        int num = index + 1;
        int left = num * 2;
        int right = num * 2 + 1;
        cb?.Invoke(index);
        FirstTraversal(left - 1, cb);
        FirstTraversal(right - 1, cb);
    }
       public void ModelTraversal(Action<int> cb)
    {
        ModelTraversal(0, cb);
    }

    private void ModelTraversal(int index, Action<int> cb)
    {
        if (index >= count || data[index].Equals(-1))
        {
            return;
        }
        int num = index + 1;
        int left = num * 2;
        int right = num * 2 + 1;
        ModelTraversal(left - 1, cb);
        cb?.Invoke(index);
        ModelTraversal(right - 1, cb);
    }
       public void EndTraversal(Action<int> cb)
    {
        EndTraversal(0, cb);
    }

    private void EndTraversal(int index, Action<int> cb)
    {
        if (index >= count || data[index].Equals(-1))
        {
            return;
        }
        int num = index + 1;
        int left = num * 2;
        int right = num * 2 + 1;
        EndTraversal(left - 1, cb);
        EndTraversal(right - 1, cb);
        cb?.Invoke(index);
    }
}
